package com.jxpanda.starter.config.security.authentication;

import org.springframework.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import reactor.core.publisher.Mono;

/**
 * @author Panda
 */
public class TokenAuthenticationManager extends UserDetailsRepositoryReactiveAuthenticationManager {

    public TokenAuthenticationManager(ReactiveUserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
        super(userDetailsService);
        this.setPasswordEncoder(passwordEncoder);
    }

    @Override
    public Mono<Authentication> authenticate(Authentication authentication) {
        if (authentication instanceof UsernamePasswordAuthenticationToken) {
            return super.authenticate(authentication);
        }
        return Mono.just(authentication);
    }

}
